package filmring

import grails.test.*

class MovieServiceTests extends GroovyTestCase {

    def movieService

    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testGetNewReleases() {
        Movie m = Movie.findByTitle("V for Vendetta")
        m.releaseDate = new Date("12/12/2012")
        m.save()
        FRUser u = FRUser.findByUsername("admin")
        Rating r = new Rating(rating: 5, lastUpdated: new Date("12/12/2012"), movie: m, user: u)
        r.save()

        List<Movie> movies = movieService.getNewReleases(5);
        assert(movies.indexOf(m) > -1)
        assert(movies.size()==5)
    }

    void testGetTopNMovies() {
        Movie m = Movie.findByTitle("V for Vendetta")
        m.releaseDate = new Date("12/12/2012")
        m.save()
        FRUser u = FRUser.findByUsername("admin")
        Rating r = new Rating(rating: 5, lastUpdated: new Date("12/12/2012"), movie: m, user: u)
        r.save()

        List<Movie> movies = movieService.getTopNMovies(10);
        assert(movies.indexOf(m) > -1)
        assert(movies.size()==10)
    }

    /*void testGetTopMovies() {
        Movie m = Movie.findByTitle("V for Vendetta")
        m.releaseDate = new Date("12/12/2012")
        m.save()
        FRUser u = FRUser.findByUsername("admin")
        Rating r = new Rating(rating: 5, lastUpdated: new Date("12/12/2012"), movie: m, user: u)
        r.save()

        List<Movie> movies = movieService.getTopMovies(10);
        assert(movies.indexOf(m) > -1)
        assert(movies.size()==10)
    }*/

    void testGetAvgRating() {

        Movie m = new Movie(director: new Director(name: "test").save(), title: "test movie")
        m.releaseDate = new Date("12/12/2012")
        m.save()
        FRUser u = FRUser.findByUsername("admin")
        Rating r = new Rating(rating: 3, lastUpdated: new Date("12/12/2012"), movie: m, user: u)
        r.save()

        assert(movieService.getAvgRating(m) == "3.0");
    }
}
